package com.gss.autotools.mouse;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.mouse.NativeMouseEvent;
import com.github.kwhat.jnativehook.mouse.NativeMouseListener;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author: DYang
 * @since: 2023/5/26
 * @description:
 */
public class MouseClickEvent {

    private static int pointX = 0;
    private static int pointY = 0;
    private static AtomicLong clickNum = new AtomicLong(0);
    private static boolean clickChange = false;

    /**
     * 注册全局事件
     */
    public static void registerGlobalMouse(){
        // 添加全局鼠标监听器
        GlobalScreen.addNativeMouseListener(new NativeMouseListener() {
            @Override
            public void nativeMouseClicked(NativeMouseEvent e) {

                clickNum.incrementAndGet();
                if (clickNum.get() > 2) {
                    if (pointX != e.getX() || pointY != e.getY()) {
                        clickChange = true;
                    }
                }
                pointX = e.getX();
                pointY = e.getY();
                System.out.println("点击次数：" + clickNum + " , 坐标：" + pointX + " , " + pointY);
            }

            @Override
            public void nativeMousePressed(NativeMouseEvent e) {
            }

            @Override
            public void nativeMouseReleased(NativeMouseEvent e) {
            }
        });
        // 初始化并启动NativeHook
        try {
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException ex) {
            System.err.println("NativeHook注册失败: " + ex.getMessage());
            System.exit(1);
        }
    }

    /**
     * 清理
     */
    public static void clear(){
        pointX = 0;
        pointY = 0;
        clickNum.set(0);
    }

    public static int getPointX() {
        return pointX;
    }

    public static int getPointY() {
        return pointY;
    }

    public static AtomicLong getClickNum() {
        return clickNum;
    }

    public static boolean isClickChange() {
        return clickChange;
    }

    public static void setClickChange(boolean clickChange) {
        MouseClickEvent.clickChange = clickChange;
    }
}
